package com.qw.flutter.nativedialog;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;

/**
 * @date: 2021/5/13 13:51
 * @author: lixu
 * @description: loading dialog弹窗管理
 */
class DialogManager implements OnDialogCancelListener {
    private final String TAG = "DialogManager";
    private final Activity activity;
    private final FlutterDialogPlugin dialogPlugin;

    /**
     * 全局通用弹窗
     */
    private LoadingDialog commonDialog;

    /**
     * http全局弹窗
     */
    private LoadingDialog globalDialog;
    /**
     * 全局弹窗显示时对应的tag
     * 如果是点击返回按钮关闭的弹窗，此时要回传全局弹窗关闭的事件到flutter，用于逻辑处理
     */
    private String globalTag;

    /**
     * 当http全局弹窗被占用时（正在显示），此时如果有显示dialog的需求，直接创建新的dialog对象
     * 多实例弹窗保存到map
     * key:tag
     * value:弹窗对象
     */
    private final HashMap<String, LoadingDialog> dialogHashMap = new HashMap<>();

    public DialogManager(Activity activity, FlutterDialogPlugin dialogPlugin) {
        this.activity = activity;
        this.dialogPlugin = dialogPlugin;
    }

    /**
     * 显示弹窗
     *
     * @param tag       当前弹窗对应的标示，如果canCancel=true，用户可以手动关闭弹窗，当关闭弹窗时，
     *                  native要通知flutter弹窗关闭事件并回传tag，flutter可以通过tag判断对应哪个业务，做相应处理
     * @param msg       显示文本
     * @param canCancel 是否能通过物理返回按钮关闭弹窗
     */
    public void showHttpDialog(@NonNull String tag, @Nullable String msg,  @NonNull String loadingStyle, boolean canCancel) {
        Log.i(TAG, "showDialog:" + tag + "  msg:" + msg + " canCancel:" + canCancel);
        if (tag.equals(globalTag)) {
            //当前要显示的dialog对应的tag与正在显示的全局dialog有相同的tag，直接覆盖之前正在显示的全局dialog
            if (globalDialog != null) {
                Log.i(TAG, "showDialog:当前tag显示的是正在显示的全局dialog");
                globalDialog.showLoadDialog(tag, msg, canCancel, loadingStyle, this);
                return;
            } else {
                globalTag = null;
            }
        }

        if (globalDialog != null && globalDialog.isShowing()) {
            //如果tag对应的dialog已经存在，先dismiss
            LoadingDialog oldDialog = dialogHashMap.remove(tag);
            if (oldDialog != null) {
                oldDialog.dismissLoadDialog(true);
                Log.i(TAG, "showDialog:显示独立dialog，对应的tag已经显示，先dismiss");
            }

            //全局弹窗被占用了，创建新的dialog对象
            LoadingDialog dialog = new LoadingDialog(activity);
            dialogHashMap.put(tag, dialog);
            dialog.showLoadDialog(tag, msg, canCancel, loadingStyle, this);

            Log.i(TAG, "showDialog:显示独立dialog，dialogHashMap.size:" + dialogHashMap.size());
        } else {
            //显示全局dialog
            if (globalDialog == null) {
                globalDialog = new LoadingDialog(activity);
            }
            globalTag = tag;
            globalDialog.showLoadDialog(tag, msg, canCancel, loadingStyle, this);
            Log.i(TAG, "showDialog:显示全局dialog");
        }
    }

    /**
     * 通过代码调用关闭弹窗
     *
     * @param tag 根据tag查找对应的dialog
     * @return 是否有dialog被关闭
     */
    public void dismissHttpDialog(@NonNull String tag) {
        Log.i(TAG, "dismissDialog:" + tag);
        if (tag.equals(globalTag)) {
            Log.i(TAG, "dismissDialog global");
            if (globalDialog != null) {
                globalDialog.dismissLoadDialog();
            }
            globalTag = null;
        } else {
            LoadingDialog dialog = dialogHashMap.remove(tag);
            Log.i(TAG, "dismissDialog dialog is null:" + (dialog == null));
            if (dialog != null) {
                dialog.dismissLoadDialog(true);
            }
        }
    }

    /**
     * 关闭所有弹窗
     * 应用退出时调用，暂时不处理逻辑
     */
    public void dismissAll() {

    }

    /**
     * dialog被物理返回按钮关闭时回调该方法
     *
     * @param tag
     */
    @Override
    public void onCancel(String tag) {
        Log.e(TAG, "onCancel弹窗被手动关闭,tag：" + tag);
        if (tag != null) {
            if (tag.equals(globalTag)) {
                globalTag = null;
            } else {
                dialogHashMap.remove(tag);
            }
            //dialog被物理返回按钮关闭时通知flutter
            if (dialogPlugin != null) {
                dialogPlugin.onBackButtonCloseDialogEvent(tag);
            }
        }

    }

    /**
     * 隐藏通用的dialog
     */
    public void dismissCommonDialog() {
        if (commonDialog != null) {
            commonDialog.dismissLoadDialog();
        }
    }

    /**
     * 显示通用的dialog
     *
     * @param msg
     * @param loadingStyle
     * @param canCancel
     */
    public void showCommonDialog(String msg,  @NonNull String loadingStyle, boolean canCancel) {
        if (commonDialog == null) {
            commonDialog = new LoadingDialog(activity);
        }
        commonDialog.showLoadDialog(null, msg, canCancel, loadingStyle, this);
    }
}
